<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="活动名称" prop="daName">
        <el-input
          v-model="queryParams.daName"
          placeholder="请输入活动名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="daStatus">
        <el-select
          v-model="queryParams.daStatus"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option label="未开始" value="0" />
          <el-option label="已开始" value="1" />
          <el-option label="已结束" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动时间" prop="activityTime">
        <el-date-picker
          v-model="dateRange"
          type="datetimerange"
          range-separator="-"
          start-placeholder="选择起始时间"
          end-placeholder="选择结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          style="width: 350px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="活动名称" align="center" prop="daName" :show-overflow-tooltip="true" />
      <el-table-column label="商家ID" align="center" prop="businessId" width="100" />
      <el-table-column label="开始时间" align="center" prop="daStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.daStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="daEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.daEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠门槛" align="center" prop="daDoorsill" width="120">
        <template #default="scope">
          <span>满{{ scope.row.daDoorsill }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="优惠金额" align="center" prop="daMoney" width="120">
        <template #default="scope">
          <span>减{{ scope.row.daMoney }}元</span>
        </template>
      </el-table-column>
      <el-table-column label="活动范围" align="center" prop="daRange" width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.daRange === 0" type="success">全品类</el-tag>
          <el-tag v-else-if="scope.row.daRange === 1" type="warning">指定商品</el-tag>
          <el-tag v-else type="info">{{ scope.row.daRange }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="daStatus" width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.daStatus === 0" type="info">未开始</el-tag>
          <el-tag v-else-if="scope.row.daStatus === 1" type="success">已开始</el-tag>
          <el-tag v-else-if="scope.row.daStatus === 2" type="warning">已结束</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            v-if="scope.row.daStatus === 1"
            link
            type="warning"
            icon="Close"
            @click="handleClose(scope.row)"
          >关闭</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="DoorsillActivity">
import { ref, reactive, toRefs, getCurrentInstance, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { listDoorsillActivity, delDoorsillActivity, closeDoorsillActivity } from "@/api/csmshop/doorsillActivity";

const { proxy } = getCurrentInstance();
const router = useRouter();

const activityList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    daName: undefined,
    daStatus: undefined,
    daStartTime: undefined,
    daEndTime: undefined
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询满额活动列表 */
function getList() {
  loading.value = true;
  listDoorsillActivity(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    activityList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  // 确保路由已添加后再跳转
  if (router.hasRoute('DoorsillActivityForm')) {
    router.push({ path: '/marketing/csmshop/doorsillActivity/form' });
  } else {
    // 如果路由还没添加，先添加再跳转
    const formRoute = {
      path: '/marketing/csmshop/doorsillActivity/form',
      component: () => import('./form.vue'),
      name: 'DoorsillActivityForm',
      meta: { title: '满额活动表单' }
    }
    router.addRoute(formRoute)
    router.push({ path: '/marketing/csmshop/doorsillActivity/form' });
  }
}

/** 查看按钮操作 */
function handleView(row) {
  reset();
  // 确保路由已添加后再跳转
  if (router.hasRoute('DoorsillActivityForm')) {
    router.push({ path: '/marketing/csmshop/doorsillActivity/form', query: { id: row.daId, type: 'view' } });
  } else {
    // 如果路由还没添加，先添加再跳转
    const formRoute = {
      path: '/marketing/csmshop/doorsillActivity/form',
      component: () => import('./form.vue'),
      name: 'DoorsillActivityForm',
      meta: { title: '满额活动表单' } 
    }
    router.addRoute(formRoute)
    router.push({ path: '/marketing/csmshop/doorsillActivity/form', query: { id: row.daId, type: 'view' } });
  }
}

/** 关闭活动操作 */
function handleClose(row) {
  proxy.$modal.confirm('是否确认关闭活动"' + row.daName + '"？').then(function() {
    return closeDoorsillActivity(row.daId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("关闭成功");
  }).catch(() => {});
}

/** 表单重置 */
function reset() {
  form.value = {
    daId: undefined,
    daName: undefined,
    daStartTime: undefined,
    daEndTime: undefined,
    daContent: undefined,
    daDoorsill: undefined,
    daMoney: undefined,
    daRange: '0',
    businessId: undefined
  };
  proxy.resetForm("formRef");
}

getList();

// 手动添加路由
onMounted(() => {
  const formRoute = {
    path: '/marketing/csmshop/doorsillActivity/form',
    component: () => import('./form.vue'),
    name: 'DoorsillActivityForm',
    meta: { title: '满额活动表单' }
  }
  
  // 检查路由是否已存在
  if (!router.hasRoute('DoorsillActivityForm')) {
    router.addRoute(formRoute)
    console.log('手动添加路由成功:', formRoute)
  } else {
    console.log('路由已存在')
  }
});
</script>
